package org.jshand.jxc.business.book.panel.booklist;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.jshand.jxc.business.book.vo.Student;

public class StudentTableModel extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Student> students;// 数据集合，即需要展示的数据集

	public StudentTableModel(List<Student> list) {// 构造方法，在创建对象时就将数据传入
		
		if(list == null){
			list = new ArrayList();
			list.add(new Student("20150101","张三","jshand@163.com"));
		}
		
		this.students = list;
	}

	@Override
	public int getRowCount() {// 行数，即数据集合List的大小
		return this.students.size();
	}

	@Override
	public int getColumnCount() {
		return 6;// 列数，固定值，即Student类有几个字段就有几列
	}

	// 这个方法是关键，给定行号及列号，返回一个元素。我们以List的下标为行号，列号对应Student的不同字段
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return rowIndex + 1 + "";
		case 1:
			return this.students.get(rowIndex).getStudentNo();
		case 2:
			return this.students.get(rowIndex).getStudentName();
		case 3:
			return this.students.get(rowIndex).getEmailAddress();
		case 4:
			return this.students.get(rowIndex).getEmailAddress();
		default:
			return "-";
		}
	}

	// 列名，给定列号，返回列名
	@Override
	public String getColumnName(int column) {
		switch (column) {
		case 0:
			return "序号";
		case 1:
			return "书籍名称";
		case 2:
			return "作者";
		case 3:
			return "出版社";
		case 4:
			return "在库数量";
		case 5:
			return "操作";
		default:
			return "-";
		}
	}

	public List<Student> getstudents() {
		return students;
	}

	public void setstudents(List<Student> students) {
		this.students = students;
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		if (columnIndex == 5) {
			return true;
		} else {
			return false;
		}
	}

}